<?php

/**
 * discussion actions.
 *
 * @package    sinc
 * @subpackage discussion
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class discussionActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
   $this->id_groupe = $request->getParameter('id_groupe');

    $c = new Criteria();
    $c->add(DiscussionPeer::ID_GROUPE,$this->id_groupe);

    $this->discussion_list = DiscussionPeer::doSelect($c);

     $this->id_user = $this->getUser()->getGuardUser()->getId();

   $c = new Criteria();
   $c->add(PagePeer::ID_USER,$this->id_user);
   $this->css = PagePeer::doSelect($c);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->id_groupe = $request->getParameter('id_groupe');

     $discussion = new Discussion();
    $discussion->setIdGroupe($this->id_groupe);

    $this->form = new DiscussionForm($discussion);

     $this->id_user = $this->getUser()->getGuardUser()->getId();

   $c = new Criteria();
   $c->add(PagePeer::ID_USER,$this->id_user);
   $this->css = PagePeer::doSelect($c);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->id_groupe = $request->getParameter('id_groupe');

    $discussion = new Discussion();
    $discussion->setIdGroupe($this->id_groupe);

    $this->form = new DiscussionForm($discussion);

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($discussion = DiscussionPeer::retrieveByPk($request->getParameter('id_discussion')), sprintf('Object discussion does not exist (%s).', $request->getParameter('id_discussion')));
    $this->form = new DiscussionForm($discussion);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($discussion = DiscussionPeer::retrieveByPk($request->getParameter('id_discussion')), sprintf('Object discussion does not exist (%s).', $request->getParameter('id_discussion')));
    $this->form = new DiscussionForm($discussion);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($discussion = DiscussionPeer::retrieveByPk($request->getParameter('id_discussion')), sprintf('Object discussion does not exist (%s).', $request->getParameter('id_discussion')));
    $discussion->delete();

    $this->redirect('discussion/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $discussion = $form->save();

      $this->redirect('@mesgroupes');
    }
  }
}
